package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductSpecService;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.entity.product.ProductSpec;
import com.atguigu.spzx.model.vo.common.R;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/admin/product/spec")
public class ProductSpecController {
    @Resource
    ProductSpecService productSpecService;
    @Operation(summary = "新增商品规格")
    @PostMapping("save")
    public R save(@RequestBody ProductSpec productSpec){
        productSpecService.save(productSpec);
        //有些需求需要返回新增成功的角色对象给前端回显(需要绑定id)
        return R.ok();
    }
    @Operation(summary = "删除指定商品规格")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable("id") Long id){
        productSpecService.deleteByIds(Arrays.asList(id));
        return R.ok();
    }
    @Operation(summary = "查询指定商品规格")
    @GetMapping("getById/{id}")
    public R getById(@PathVariable("id") Long id){
        ProductSpec productSpec = productSpecService.getById(id);
        return R.ok("item",productSpec);
    }
    @Operation(summary = "更新指定商品规格")
    @PutMapping("update")
    public R update(@RequestBody ProductSpec productSpec){
        productSpecService.update(productSpec);
        //有些需求需要返回新增成功的角色对象给前端回显(需要绑定id)
        return R.ok();
    }
    @Operation(summary = "查询商品规格分页数据")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R list(@PathVariable("pageNum") Integer pageNum ,
                  @PathVariable("pageSize") Integer pageSize){
        PageInfo<ProductSpec> pageInfo =  productSpecService.list(pageNum,pageSize);
        return R.ok("page" , pageInfo);
    }

    @Operation(summary = "查询所有商品规格")
    @GetMapping("list")
    public R list(){
        List<ProductSpec> list =  productSpecService.list();
        return R.ok("items" , list);
    }
}
